package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    @RequiredPermission(name = "员工列表", expression = "employee:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo){
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("departments", departmentService.selectAll());
        return "employee/list";
    }

    @RequiredPermission(name = "员工删除", expression = "employee:delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        if(id != null){
            employeeService.delete(id);
        }
        return "redirect:list";
    }

    @RequiredPermission(name = "员工新增/编辑页面", expression = "employee:input")
    @RequestMapping("/input")
    public String input(Long id, Model model){
        if(id != null){
            Employee employee = employeeService.get(id);
            model.addAttribute("employee", employee);
        }
        model.addAttribute("departments", departmentService.selectAll());
        model.addAttribute("roles", roleService.selectAll());
        return "employee/input";
    }

    @RequiredPermission(name = "员工新增/编辑", expression = "employee:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Employee employee, Long[] roleIds){
        if (employee.getId() != null){
            employeeService.update(employee, roleIds);
        }else{
            employeeService.insert(employee, roleIds);
        }
        return "redirect:list";
    }

    @RequiredPermission(name = "导出员工信息表", expression = "employee:exportXls")
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        //设置文件名称支持中文
        String fileName =new String("员工信息表.xls".getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1);
        //文件下载的响应头（让浏览器访问资源的的时候以下载的方式打开）
        response.setHeader("Content-Disposition","attachment;filename="+ fileName);
        Workbook wb = employeeService.exportXls();
        //响应浏览器
        wb.write(response.getOutputStream());
    }

    @RequiredPermission(name = "导入员工信息表", expression = "employee:importXls")
    @RequestMapping("/importXls")
    public String importXls(MultipartFile file){
       employeeService.importXls(file);
       return "redirect:/employee/list";
    }

}
